#include "randnum.h"
#include <windows.h>
#include <sortthread.h>

void SortThread::insertSort(SortObject * pvector)
{
    sortTle = 0;
    sortMov = 0;
    sortCmp = 0;
    int i, j;
    pvector->n = this->size;
    RecordNode temp;
    RecordNode *data = pvector->record;
    for (i = 1; i < pvector->n; i++)
    {
        sortTle++;
        this->sendt(sortTle,sortMov,sortCmp);
        temp = data[i];
        for (j = i - 1; temp.key < data[j].key && j >= 0; j--)
        {
            sortCmp++;
            data[j + 1] = data[j];
            for(int ii = 0; ii < pvector->n; ii++)
            {
                this->sendArray[ii] = data[ii].key;
            }
            this->senda(i,j);
            this->sendt(sortTle,sortMov,sortCmp);
        }
        if (j != i - 1)
        {
            sortCmp++;
            data[j + 1] = temp;
            sortMov++;
            for(int ii = 0; ii < pvector->n; ii++)
            {
                this->sendArray[ii] = data[ii].key;
            }
            this->sendt(sortTle,sortMov,sortCmp);
        }
        //this->sendArray[i] = data[i].key;
        this->senda(i,j);
    }
    for(int i = 0; i < size; i++)
    {
        senda(i,-10);
    }
    this->sendt(sortTle,sortMov,sortCmp);
}
